package com.antonytrupe.fatcontroller.pc.ui;

import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;

import com.antonytrupe.fatcontroller.EngineListView;
import com.antonytrupe.fatcontroller.Engine;

public class EngineControlPanel extends JPanel implements ActionListener,
		com.antonytrupe.fatcontroller.ui.EngineControlPanel {
	private static final long serialVersionUID = 854749397968428321L;
	private static final String BACKWARD = "backward";
	private static final String STOP = "stop";
	private static final String FORWARD = "forward";
	private static final String ENGINE_LIST = "engine_list";
	private Engine engine;
	private EngineListView controller;

	public EngineControlPanel(Engine engine, EngineListView controller) {
		this.controller = controller;
		this.engine = engine;
		setLayout(new GridLayout(0, 1));
		JPanel controlPanel = new JPanel();
		controlPanel.setLayout(new GridLayout(0, 3));
		add(controlPanel);
		JLabel nameLabel = new JLabel(engine.name);
		add(nameLabel);
		JLabel videoLabel = new JLabel("VIDEO");
		videoLabel.setSize(200, 200);
		videoLabel.setBorder(BorderFactory.createLineBorder(Color.black));
		add(videoLabel);
		try {
			JButton backwardLabel = new JButton(new ImageIcon(
					ImageIO.read(new File("backward.png")), "BACKWARD"));
			controlPanel.add(backwardLabel);
			backwardLabel.setBorder(null);
			backwardLabel.setActionCommand(BACKWARD);
			backwardLabel.addActionListener(this);

			JButton stopLabel = new JButton(new ImageIcon(
					ImageIO.read(new File("stop-sign.png")), "STOP"));
			controlPanel.add(stopLabel);
			stopLabel.setBorder(null);
			stopLabel.setActionCommand(STOP);
			stopLabel.addActionListener(this);

			JButton forwardLabel = new JButton(new ImageIcon(
					ImageIO.read(new File("forward.png")), "FORWARD"));
			controlPanel.add(forwardLabel);
			forwardLabel.setBorder(null);
			forwardLabel.setActionCommand(FORWARD);
			forwardLabel.addActionListener(this);
		} catch (IOException e) {
			e.printStackTrace();
		}
		JButton engineList = new JButton("Back to list of engines");
		engineList.setActionCommand(ENGINE_LIST);
		engineList.addActionListener(this);
		add(engineList);
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		if (e.getActionCommand() == null) {
		} else if (e.getActionCommand().equals(STOP)) {
			this.stop();
		} else if (e.getActionCommand().equals(FORWARD)) {
			this.forward();
		} else if (e.getActionCommand().equals(BACKWARD)) {
			this.backward();
		} else if (e.getActionCommand().equals(ENGINE_LIST)) {
			this.displayEngines();
		}
	}

	@Override
	public void displayEngines() {
		controller.displayEngines();
	}

	@Override
	public void backward() {
		controller.backward(this.engine);
	}

	@Override
	public void forward() {
		controller.forward(this.engine);
	}

	@Override
	public void stop() {
		controller.stop(this.engine);
	}
}